import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/home/user_detail_entity.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/widgets/header_delegate.dart';

import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class ExpertLeaguesPage extends StatefulWidget {
  const ExpertLeaguesPage({super.key});

  @override
  State<ExpertLeaguesPage> createState() => _ExpertLeaguesPageState();
}

class _ExpertLeaguesPageState extends State<ExpertLeaguesPage> {
  UserDetailEntity? user = Get.arguments;
  List<LeagueTop>? data;
  List<ExpertLeagueItem>? items;
  List<String> titles = ["联赛", "发文数", "命中率"];

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async {
    data = await ExpertApi.getExpertLeagues(user?.userId ?? "");
    items = data?.map((e) => ExpertLeagueItem.from(user, e)).toList() ?? [];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: Styles.appBar(
          backgroundColor: Colours.transparent,
          systemUiOverlayStyle: const SystemUiOverlayStyle(
            statusBarIconBrightness: Brightness.light,
          ),
          leadingColor: Colors.white,
          title: QsText("联赛分布", color: Colors.white)),
      body: Stack(
        children: [
          Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Image.asset(Utils.getImgPath("user_header_bg.png"),
                  fit: BoxFit.fill)),
          Positioned.fill(
            top: MediaQuery.of(context).padding.top + 44,
            child: Container(
                clipBehavior: Clip.antiAlias,
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius:
                        BorderRadius.vertical(top: Radius.circular(8))),
                child: CustomScrollView(
                  slivers: [
                    SliverPersistentHeader(
                        pinned: true,
                        delegate: HeaderDelegate(
                          child: Container(
                            color: Colors.white,
                            height: 40,
                            margin: EdgeInsets.symmetric(horizontal: 26),
                            child: Row(
                                children: titles
                                    .map<Widget>((e) => Expanded(
                                          child: QsText(
                                            e,
                                            color: Colours.grey99,
                                            textAlign: e == "发文数"
                                                ? TextAlign.center
                                                : e == "联赛"
                                                    ? TextAlign.left
                                                    : TextAlign.right,
                                          ),
                                        ))
                                    .toList()),
                          ),
                          extent: 40,
                        )),
                    SliverList.list(
                        children: items
                                ?.map<Widget>((e) => GestureDetector(
                                      behavior: HitTestBehavior.translucent,
                                      onTap: () => Get.toNamed(
                                          Routes.expertLeagueList,
                                          arguments: e),
                                      child: item(e)
                                          .paddingSymmetric(horizontal: 26),
                                    ))
                                .toList()
                                .insertEvery(const Divider(
                                        color: Colours.greyEE, height: 0.5)
                                    .paddingSymmetric(horizontal: 16)) ??
                            [])
                  ],
                )),
          )
        ],
      ),
    );
  }

  Widget item(ExpertLeagueItem? item) {
    return DefaultTextStyle(
      style: const TextStyle(fontSize: 14, color: Colours.text),
      child: SizedBox(
        height: 40,
        child: Row(children: [
          Expanded(
              child: QsText(
            item?.leagueName ?? "",
            needCenter: true,
          ).alignStart),
          Expanded(
              child: QsText(item?.content ?? "-", needCenter: true).center),
          Expanded(
              child: QsText(item?.hitRatio ?? "-",
                      needCenter: true,
                      fontSize: 20,
                      color: Colours.main,
                      fontFamily: "Din")
                  .alignEnd),
        ]),
      ),
    );
  }
}
